package br.com.pucrs.tcc.util.jsf;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.com.pucrs.tcc.enumerator.TipoUsuarioEnum;
import br.com.pucrs.tcc.login.SessionType;

public class SessionUtil {

	public static String getMatriculaUsuarioLogado() {
		FacesContext fContext = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) fContext.getExternalContext().getRequest();
		HttpSession httpSession = request.getSession(false);

		SessionType session = null;

		if (httpSession.getAttribute("user-session") != null) {
			session = (SessionType) httpSession.getAttribute("user-session");
		}

		return session.getMatricula();
	}

	public static TipoUsuarioEnum getTipoUsuarioLogado() {
		FacesContext fContext = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) fContext.getExternalContext().getRequest();
		HttpSession httpSession = request.getSession(false);

		SessionType session = null;

		if (httpSession.getAttribute("user-session") != null) {
			session = (SessionType) httpSession.getAttribute("user-session");
		}

		return session.getTipoUsuario();
	}

	public static boolean temSessaoAtiva() {
		FacesContext fContext = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) fContext.getExternalContext().getRequest();
		HttpSession httpSession = request.getSession(false);

		if (httpSession.getAttribute("user-session") != null) {
			return true;
		}

		return false;
	}
}
